//  Task ventilator
//  Binds PUSH socket to tcp://localhost:5557
//  Sends batch of tasks to workers via that socket

#include "msg_dcp.h"
#include "czmq.h"

int main (void) 
{
    void *context = zmq_init(1);
    DCP_MESSAGE *dmsg,dhead;

    //  Socket to send messages on
    void *sender = zmq_socket(context, ZMQ_PUB);

    dmsg =(DCP_MESSAGE *)calloc(1,sizeof(DCP_MESSAGE));
    char *string =(char *)calloc(256,sizeof(char));

    long int linger;
    linger = 3000;
    zmq_setsockopt(sender, ZMQ_LINGER, &linger, sizeof(unsigned long int));
    linger = 4096;
    zmq_setsockopt(sender, ZMQ_SNDHWM, &linger, sizeof(unsigned long int));
    zmq_setsockopt(sender, ZMQ_RCVHWM, &linger, sizeof(unsigned long int));

    zmq_bind(sender, "tcp://*:5557");

    printf ("Press Enter tcp://*:5557 when the workers are ready: ");
    getchar ();
    printf ("Sending tasks to workers...\n");

    //  Initialize random number generator
    srandom ((unsigned) time (NULL));

    //  Send 100 tasks
    int count=0;
    int total_msec = 0;     //  Total expected cost in msecs

    char *encmsg;
    char sname[16];

    while(count<10) {
        int workload=0x1234;
        workload = randof (100) + 1;

	int enclen;

	uint64_t xlong=0x987654321;

	//memset(string+5,0x0,sizeof(char)*3);
	dhead.wCode=0x1234;
	dhead.wFlags=0x4321;
   	dhead.dwSize = 0xFFAA5511;
   	dhead.dwId=0x22aa5522;       
   	dhead.dwNumVars=0x3399aa55;

	memcpy(string,&dhead,sizeof(dhead));   //version == 2
	
	memcpy(string+sizeof(DCP_MESSAGE),&xlong,sizeof(uint64_t));

	printf("send %d len \n",(int)(sizeof(DCP_MESSAGE)+sizeof(uint64_t)));

        zmq_send(sender,string,(size_t)sizeof(DCP_MESSAGE)+sizeof(uint64_t),ZMQ_DONTWAIT);
	
	count++;
    }
    printf ("Total expected cost: %d msec\n", total_msec);

    zmq_close(sender);
    zmq_term(context);
   
    return 0;
}
